package akbderby.page;

import java.util.List;

import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.util.RequestMap;

import scenic3.annotation.ActionPath;
import scenic3.annotation.Default;
import scenic3.annotation.Page;
import akbderby.meta.TweetMeta;
import akbderby.model.Tweet;
import akbderby.service.TwitterService;

@Page("/twitter")
public class TwitterPage extends BasePage {
    private TwitterService service = new TwitterService();

    @ActionPath("tweet")
    public Navigation tweet() {
        if (!validate()) {
            return redirect(basePath);
        }
        service.tweet(new RequestMap(request), getUser());
        return redirect(basePath);
    }

    // @ActionPath("upload")
    // public Navigation upload() throws Exception {
    // BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
    // Map<String, BlobKey> blobs = bs.getUploadedBlobs(request);
    // BlobKey blobKey = blobs.get("formFile");
    // if (blobKey != null) {
    // Key key =
    // Datastore.createKey(Blobstore.class, blobKey.getKeyString());
    // Blobstore blobstore = new Blobstore();
    // blobstore.setKey(key);
    // Datastore.put(blobstore);
    // }
    // return redirect(basePath);
    // }

    // @ActionPath("show/{id}")
    // public Navigation show(@Var("id") String id) throws Exception {
    // BlobKey blobKey = new BlobKey(id);
    // BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
    // bs.serve(blobKey, response);
    // return null;
    // }
    // @ActionPath("delete/{id}")
    // public Navigation delete(@Var("id") String id) throws Exception {
    // BlobKey blobKey = new BlobKey(id);
    // BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
    // bs.delete(blobKey);
    // Datastore.delete(Datastore.createKey(Blobstore.class, id));
    // return redirect(basePath);
    // }

    @Default
    public Navigation index() {
        List<Tweet> tweetList = service.getTweetList();
        // List<Tweet> tweetList = new ArrayList<Tweet>();
        requestScope("tweetList", tweetList);
        return forward("/twitter/tweetList.jsp");
    }

    protected boolean validate() {
        TweetMeta meta = TweetMeta.get();
        Validators v = new Validators(request);
        v.add(meta.content, v.required());
        return v.validate();
    }
}
